/*
@Copyright:LintCode
@Author:   tjyemail
@Problem:  http://www.lintcode.com/problem/evaluate-reverse-polish-notation
@Language: C++
@Datetime: 16-02-09 05:48
*/

class Solution {
public:
    /**
     * @param tokens The Reverse Polish Notation
     * @return the value
     */
    int evalRPN(vector<string>& tokens) {
        // Write your code here
        stack<int> s;
        int num;
        for(int i=0;i<tokens.size();++i){
            if (tokens[i].length()==1){
            switch(tokens[i][0]){
            case '+':
                num = s.top();
                s.pop();
                num += s.top();
                s.pop();
                s.push(num);
                break;
            case '-':
                num = s.top();
                s.pop();
                num = s.top()-num;
                s.pop();
                s.push(num);
                break;
            case '*':
                num = s.top();
                s.pop();
                num *= s.top();
                s.pop();
                s.push(num);
                break;
            case '/':
                num = s.top();
                s.pop();
                num = s.top()/num;
                s.pop();
                s.push(num);
                break;
            default:
                sscanf(tokens[i].c_str(),"%d",&num);
                s.push(num);
            }
            }
            else{
                sscanf(tokens[i].c_str(),"%d",&num);
                s.push(num);
            }
        }
        num = s.top();
        return num;
    }
};